package com.cskaoyan.servlet;


import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.service.AdminCommentService;
import com.cskaoyan.service.AdminCommentServiceImpl;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/admin/comment/*")
public class AdminCommentServlet extends WdBaseServlet {
    AdminCommentService adminCommentService = new AdminCommentServiceImpl();

    private BaseRespVo list(HttpServletRequest request, HttpServletResponse response) {
        String pageStr = request.getParameter("page");
        int page = Integer.parseInt(pageStr);

        String limitStr = request.getParameter("limit");
        int limit = Integer.parseInt(limitStr);

        String userIdStr = request.getParameter("userId");
        if ("".equals(userIdStr)){
            userIdStr = null;
        }
        Integer userId = userIdStr == null  ? null : Integer.parseInt(userIdStr);

        String valueIdStr = request.getParameter("valueId");
        if ("".equals(valueIdStr)){
            valueIdStr = null;
        }
        Integer valueId = valueIdStr == null ? null : Integer.parseInt(valueIdStr);

        String sort = request.getParameter("sort");
        String order = request.getParameter("order");

        BaseRespVo baseRespVo = adminCommentService.list(page, limit, userId, valueId, sort, order);
        return baseRespVo;

    }

    @SneakyThrows
    private BaseRespVo delete(HttpServletRequest request, HttpServletResponse response) {
        String s = request.getReader().readLine();
        //值在请求体的json中
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree(s);
        String idStr = jsonNode.get("id").asText();
        int id = Integer.parseInt(idStr);
        BaseRespVo baseRespVo = adminCommentService.delete(id);

        return baseRespVo;
    }

}
